/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    var toastr =require('toastr');
    app.useModule("ui.table");


    var datepicker = require('datepicker');
    app.controller('account_bankCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {

        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };

        $scope.ctrlScope=$scope;
        $scope.orderParams = {
            url: "/admin/accountbank/list"
        };
        $scope.list={};
        datepicker($scope);
        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime= $scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={};
            $scope.orderParams.reload();
        };

       /* //银行卡编辑
        $scope.modifyModal = function(id){
        	 $scope.detailsModal= !$scope.detailsModal;
        	 $scope.getOne(id);
        };*/
       	//单个查询
        $scope.getOne = function(id){
            $http({
                method: 'POST',
                url: "/admin/accountbank/getSingle",
                data: {
                	id:id,
                
                }
            }).success(function(data) {
                console.info(data);
                 $scope.list=data;
            }).error(function(data) {
            });
        };
       /* //保存
        $scope.saveSubmit = function(){
        	var parm=$scope.list;
            $http({
                method: 'POST',
                url: "/admin/accountbank/edit",
                data:parm,
            }).success(function(data) {

                toastr.success("保存成功");
                $scope.detailsModal= !$scope.detailsModal;
                $scope.orderParams.reload();

            }).error(function(data) {
            });
        };*/


        ////银行卡删除
        //$scope.deleteModal = function(id){
        //	if (confirm("确认删除此银行卡？")) {
        //		$http({
        //        method: 'POST',
        //        url: "/admin/accountbank/del",
        //        data: {
        //        	id:id
        //        }
        //    }).success(function(data) {
        //        console.info(data);
        //            toastr.success("删除成功");
        //        $scope.orderParams.reload();
        //    }).error(function(data) {
        //    });
        //	}else{
        //		return;
        //	}
        //
        //};

        $scope.openView=function(userId){
            $scope.bank = {};
            $scope.queryBank(userId);
            $scope.viewModal = !$scope.viewModal;
        };

        //查询借款信息
        $scope.queryBank = function(userId) {
            $http({
                method: 'POST',
                url: "/admin/order/cardSearch",
                data: {
                    userId: userId
                }
            }).success(function(data) {
                if(data.result == 'success'){
                    $scope.bank = data.bankCards[0];
                }
            }).error(function(data) {
            });
        };


        //是否限制提现
        $scope.openlimit=function(id,status){
            if(status==1){
                $scope.islimitChecked=true;
                $scope.isNotlimitChecked=false;
            }else if(status==0){
                $scope.islimitChecked=false;
                $scope.isNotlimitChecked=true;
            }
            $scope.limitModel=!$scope.limitModel;

            $scope.limit= function () {
                var parm={
                    userId:id,
                    status:$scope.limit.status
                };
                console.log(parm);
                $http({
                    method: 'POST',
                    url: "/admin/user/modifyCashLimit",
                    data:parm
                }).success(function(data) {
                    if(data.result=='success'){
                        toastr.success('修改成功');
                        $scope.limitModel=!$scope.limitModel;
                        $scope.orderParams.ajaxParam={type:'1'};
                        $scope.orderParams.reload();

                    }else{
                        toastr.error('修改失败')
                    }

                }).error(function(data) {});
            };

        };


        //导出
        $scope.download=function(){
            var params=$scope.ctrlScope.itemsearch;
            $http.post("/admin/accountbank/excel/accountBank",params)
                .success(function (data){
                    console.info(data);
                    if(data.file){
                        window.open(data.file);
                        toastr.success("导出成功");
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };



        //新建
        $scope.addBank=function(){
            $scope.addModal = !$scope.addModal ;
            $scope.add = {};
            $scope.userId=  {};
        };

        //确定
        $scope.addBtn=function(){
            $scope.addModal = !$scope.addModal;
            var params=$scope.add.userId;
            $http.post("/admin/accountbank/addBank",params)
                .success(function (data){
                    console.info(data);
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };


    }]);


});